package com.neusoft.movie.v_movies.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.neusoft.movie.v_movies.R;
import com.neusoft.movie.v_movies.common.Constants;
import com.neusoft.movie.v_movies.javabean.ActivityBean;
import com.neusoft.movie.v_movies.javabean.ResultBean;
import com.neusoft.movie.v_movies.listener.RetrofitListener;
import com.neusoft.movie.v_movies.model.ActivityModel;
import com.squareup.picasso.Picasso;

import static com.neusoft.movie.v_movies.common.Constants.PHOTO_URL;

public class EnterActActivity extends BaseActivity implements RetrofitListener {

    private Context mContext;
    private String result;
    private int u_id,act_id;
    private TextView act_name;
    private EditText e_name,e_email,e_address,e_remarks;
    private Button button_enter;
    private ImageView left;
    @Override
    void initViews() {
        setLayout(R.layout.activity_enter_act);

        e_name = (EditText) findViewById(R.id.e_name);
        e_email = (EditText) findViewById(R.id.e_email);
        e_address = (EditText) findViewById(R.id.e_address);
        e_remarks = (EditText) findViewById(R.id.e_remarks);
        act_name = (TextView) findViewById(R.id.act_name);
        button_enter=(Button) findViewById(R.id.button_enter);
        left=(ImageView) findViewById(R.id.left_button);

    }

    @Override
    void initData() {
        act_id=getIntent().getIntExtra("act_id",-1);

        SharedPreferences sharedPreferences=getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        u_id=sharedPreferences.getInt("u_id",0);
        getDetail();

    }

    void getDetail()
    {
        ActivityModel activityModel = new ActivityModel();
        activityModel.getDetail(act_id,this);
    }
    private void enterActivity()
    {
        ActivityModel activityModel = new ActivityModel();
        activityModel.enterActivity(u_id,
                act_id,
                e_name.getText().toString(),
                e_email.getText().toString(),
                e_address.getText().toString(),
                e_remarks.getText().toString(),this);
    }

    @Override
    void initEvents() {
        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        button_enter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                enterActivity();
            }
        });

    }

    @Override
    public void onSuccess(Object o, int flag) {
        switch(flag){
            case Constants.GETACTIVITYDETAIL:
                ActivityBean activityBean=(ActivityBean) o;
                act_name.setText(activityBean.getAct_name());
                break;

            case Constants.ENTERACTIVITY:
                ResultBean resultBean=(ResultBean) o;
                if ("1".equals(resultBean.getSuccess())) {
                    Toast.makeText(EnterActActivity.this, "报名成功", Toast.LENGTH_LONG).show();
                    finish();

                }else if("2".equals(resultBean.getSuccess())){
                    Toast.makeText(EnterActActivity.this, "请不要重复报名", Toast.LENGTH_LONG).show();
                    finish();
                }
                else {
                    Toast.makeText(EnterActActivity.this, "报名失败", Toast.LENGTH_LONG).show();
                }

        }
    }

    @Override
    public void onFailed() {

    }
}
